from utils.network import Interface
from utils.model import Message, User
from utils import memory


class API(Interface):
    def handle(self, req):
        msgs = []
        for account in req['recipients']:
            if account in memory.users:
                recipient = memory.users[account]
                assert isinstance(recipient, User)
                message = Message(self.user.account, account, req['text'])
                recipient.recv_messages.append(message)
                msgs.append(message.as_view())
        return self.success_respond({
            'messages': msgs
        })
